#include "mainscene.h"
#include "ui_mainscene.h"
#include "mypushbutton.h"
#include<QPainter>
#include<QTimer>
#include<QSound>
#include<QDebug>
#include<QFile>
MainScene::MainScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainScene)
{
    ui->setupUi(this);

    //0. menu button action bind.
    connect(ui->actionCreate, &QAction::triggered, [=]() {
        qDebug() << "新建功能开发中";
    });
    connect(ui->actionExport, &QAction::triggered, [=]() {
        qDebug() << "导出功能开发中";
    });
    connect(ui->actionImport, &QAction::triggered, [=]() {
        qDebug() << "导入功能开发中";
    });
    connect(ui->actionQuit, &QAction::triggered, [=]() {
        this->close();
    });

    //设置固定大小
//    this->setFixedSize(720, 588);

    //标题
    this->setWindowTitle("ChangeHost!0.0.1.1");

    //图片
    this->setWindowIcon(QIcon(":/res/app_title.jpg"));

    //设置tree widget标题
    ui->treeWidget->setHeaderLabels(QStringList() << "名称" << "Host");

    QTreeWidgetItem  * localHost = new QTreeWidgetItem(QStringList()<<"本地方案");
    localHost->setWhatsThis(0, QString("100000"));
    QTreeWidgetItem  * commonUseHost = new QTreeWidgetItem(QStringList()<<"公用 hosts");
    QTreeWidgetItem  * currentSystemHost = new QTreeWidgetItem(QStringList()<<"当前系统 hosts");

    //添加root
    ui->treeWidget->addTopLevelItem(localHost);
    ui->treeWidget->addTopLevelItem(commonUseHost);
    ui->treeWidget->addTopLevelItem(currentSystemHost);

    //当前host内添加子节点
    QStringList hostLine1;
    QStringList hostLine2;
    QStringList hostLine3;
    QStringList hostLine4;

    hostLine1 << "开发";
    hostLine2 << "测试";
    hostLine3 << "预发布";
    hostLine4 << "现网";
    QTreeWidgetItem * currentSystemHostChild1 = new QTreeWidgetItem(hostLine1);
    currentSystemHostChild1->setWhatsThis(0,QString("0"));
    QTreeWidgetItem * currentSystemHostChild2 = new QTreeWidgetItem(hostLine2);
    currentSystemHostChild2->setWhatsThis(0,QString("2"));
    QTreeWidgetItem * currentSystemHostChild3 = new QTreeWidgetItem(hostLine3);
    currentSystemHostChild3->setBackgroundColor(0, QColor(170,0,0));
    currentSystemHostChild3->setWhatsThis(0,QString("3"));
    QTreeWidgetItem * currentSystemHostChild4 = new QTreeWidgetItem(hostLine4);
    currentSystemHostChild4->setBackgroundColor(0, QColor(150,0,0));
    currentSystemHostChild4->setWhatsThis(0,QString("4"));

    currentSystemHost->addChild(currentSystemHostChild1);
    currentSystemHost->addChild(currentSystemHostChild2);
    currentSystemHost->addChild(currentSystemHostChild3);
    currentSystemHost->addChild(currentSystemHostChild4);



    //1. 点击某个层展示host配置
    connect(this->ui->treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(slotOpen(QTreeWidgetItem*, int)));

    //C:/Windows/System32/drivers/etc 点击
    connect(ui->applyBtn, &QCommandLinkButton::clicked, [=](){
       qDebug()<<"apply click";

       //将textEdit里的内容写到c盘的host
        QString editContent = ui->textEdit->toPlainText();

        //写入文件
        QString filePath = QString("C:/Windows/System32/drivers/etc/hosts");
        QFile file(filePath);
        file.open(QIODevice::WriteOnly);
        qint64 len = -1;
        len = file.write(editContent.toUtf8(), editContent.length());
        if (len == -1) {
            qDebug()<<"写入失败";
        } else {
            qDebug()<<"写入成功";
        }
        file.close();
    });



//    //开始按钮
//    MyPushButton * startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
//    startBtn->setParent(this);
//    startBtn->move(this->width()*0.5 - startBtn->width()*0.5, this->height()*0.7);


//    //准备开始按钮的音效
//    QSound * startSound = new QSound(":/res/TapButtonSound.wav", this);
//    startSound->setLoops(5);//循环次数,-1标识无限循环

//    //创建出选择关卡的场景
//    chooseScene = new ChooseLevelScene;

//    //监听第二个选择关卡中的返回按钮信号
//    connect(chooseScene, &ChooseLevelScene::chooseSceneBack, [=](){
//       chooseScene->hide();
//       this->setGeometry( chooseScene->geometry());
//       this->show();
//    });

//    connect(startBtn, &MyPushButton::clicked, [=]() {
////        qDebug() << "点击了开始";

//        //播放音效
//        startSound->play();

//        //点击按钮，有弹跳效果
//        startBtn->Zoom1();
//        startBtn->Zoom2();

//        //延时特效 进入第二个场景
//        QTimer::singleShot(500, this, [=]() {
//            //将自身隐藏
//            this->hide();

//            //设置出现的位置
//            chooseScene->setGeometry( this->geometry());

//            //进入第二个关卡场景
//            chooseScene->show();
//        });

//    });


}

MainScene::~MainScene()
{
    delete ui;
}

void MainScene::paintEvent(QPaintEvent *event)
{
//    QPainter painter(this);

//    QPixmap pix;
//    pix.load(":/res/PlayLevelSceneBg.png");

//    painter.drawPixmap(0, 0, this->width(), this->height(), pix);


//    //画标题图片
//    pix.load(":/res/Title.png");

//    pix = pix.scaled(pix.width()*0.5, pix.height()*0.5);

//    painter.drawPixmap(10, 30, pix);
}

void MainScene::slotOpen(QTreeWidgetItem* item, int columns) {

    qDebug() << "槽函数触发" << item->text(columns)<<columns<<  ui->treeWidget->currentItem()->childCount() << ui->treeWidget->currentItem()->whatsThis(0);

    QString filePath = QString("");
    if (ui->treeWidget->currentItem()->whatsThis(0) == "100000") {
        //加载当前系统的hosts
        filePath = QString("C:/Windows/System32/drivers/etc/hosts");
    } else {
         filePath = QString(":/res/"+ui->treeWidget->currentItem()->whatsThis(0)+".hosts");
    }

    //加载hosts文件
    QFile file(filePath);
    file.open(QIODevice::ReadOnly);
    QByteArray arr = file.readAll();

    //重设右侧展示栏
    ui->textEdit->setText(arr);
}
